package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Describe implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Describe.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Describe();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (pc == null)
        _log.warn("系统命令执行: " + cmdName + " " + arg + " 显示人物附加属性。"); 
      ArrayList<String> msg = new ArrayList<>();
      L1PcInstance target = World.get().getPlayer(arg);
      if (pc == null) {
        if (target == null) {
          _log.error("指令异常: 指定人物不在线上，这个命令必须输入正确人物名称才能执行。");
          return;
        } 
      } else if (target == null) {
        target = pc;
      } 
      msg.add("-- 显示资讯人物: " + target.getName() + " --");
      msg.add("伤害附加: +" + target.getDmgup());
      msg.add("命中附加: +" + target.getHitup());
      msg.add("抗魔: " + target.getMr() + "%");
      int hpr = target.getHpr() + target.getInventory().hpRegenPerTick();
      int mpr = target.getMpr() + target.getInventory().mpRegenPerTick();
      msg.add("HP额外回复量: " + hpr);
      msg.add("MP额外回复量: " + mpr);
      msg.add("有好度: " + target.getKarma());
      msg.add("背包物品数量: " + target.getInventory().getSize());
      if (pc == null) {
        String items = "";
        Iterator<L1ItemInstance> iterator = target.getInventory().getItems().iterator();
        while (iterator.hasNext()) {
          L1ItemInstance item = iterator.next();
          items = String.valueOf(String.valueOf(items)) + "[" + item.getNumberedName(item.getCount()) + "]";
        } 
        msg.add(items);
      } 
      Iterator<String> iterator2 = msg.iterator();
      while (iterator2.hasNext()) {
        String info = iterator2.next();
        if (pc == null) {
          _log.info(info);
          continue;
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, info));
      } 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
